package com.asset.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.asset.pojo.AssetOperate;
import com.asset.pojo.AssetUseHistory;
import com.asset.pojo.UserOperate;
import com.asset.webBean.AssetOperateWeb;
import com.asset.webBean.UserOperateWeb;

import java.text.SimpleDateFormat;
import java.util.*;

public class OperateAction extends BaseAction {

	private static final long serialVersionUID = 1L;
    
	private AssetOperate assetoperate;
	private AssetOperateWeb assetOperateweb;
	private UserOperate useroperate;
	private UserOperateWeb userOperateweb;
	private Long id;
		
	private Short opteGenre;
	private List<AssetOperate> assetOperateList;
	private List<UserOperate> userOperateList;
	private List<AssetUseHistory> assetUseHistoryList;
	private JSONObject tableData;
	// 表格查询
	private String temp;
	private Integer draw;
	private Integer length;
	private Integer start;
	private Date opteTime1;
	private Date opteTime2;

	// 操作结果信息返回
	private String operationResult;

	//资产日志信息类型转换
	private AssetOperateWeb passAssetOperateWeb(AssetOperate o){
		AssetOperateWeb ow = new AssetOperateWeb();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ow.setId(o.getId());
		ow.setOptePerson(o.getOptePerson());
		if(o.getOpteGenre() != null){
			ow.setOpteGenre(AssetOperate.OpteGenre.getName(o.getOpteGenre()));
		}
		ow.setEquipIdentifier(o.getEquipIdentifier());
		ow.setEquipName(o.getEquipName());
		ow.setEquipModel(o.getEquipModel());
		ow.setEquipBrand(o.getEquipBrand());
		ow.setOpteTime(sdf.format(o.getOpteTime()));
		ow.setOpteRemark(o.getOpteRemark());
		return ow;	
	}
	
	//资产操作日志明细
	public String assetOperateInfo(){
		try {
			assetoperate = assetopteService.getAssetOpteByKey(id);
			assetOperateweb = passAssetOperateWeb(assetoperate);
		} catch (Exception e) {
			e.printStackTrace();
			getSession().setAttribute("e", e.getMessage());
			return "sysError";			
		}
		
		return SUCCESS;
		
	}
	
	//页面刷新
	public String listOperate() {
		try{
			getSession().setAttribute("colId", 4);
			temp=getTemp();
			if(getSession().getAttribute("tip") != null && getSession().getAttribute("tip")!=""){
				tip=(String) getSession().getAttribute("tip");
				getSession().setAttribute("tip", "");
			};
		}catch(Exception e){
			e.printStackTrace();
			getSession().setAttribute("e", e.getMessage());
			return "sysError";
		}
		return SUCCESS;
	}

	//资产操作日志列表界面数据加载
		public String assetListOperateData() {
			
			assetOperateList = assetopteService.listAssetOptePage(temp, start, length,opteGenre,opteTime1,opteTime2);
			Integer recordsTotal = assetopteService.listAssetOptePage(temp,null, null,opteGenre,null,null).size();
			tableData = new JSONObject();
			JSONArray operateJsonArr = new JSONArray();
			for (AssetOperate operate : assetOperateList) {
				JSONObject operateJSON = new JSONObject();
				operateJSON.put("id", operate.getId());
				operateJSON.put("optePerson", operate.getOptePerson());
				operateJSON.put("opteGenre", AssetOperate.OpteGenre.getName(operate.getOpteGenre()));
				operateJSON.put("equipIdentifier", operate.getEquipIdentifier());
				operateJSON.put("equipName", operate.getEquipName());
				operateJSON.put("equipModel", operate.getEquipModel());
				operateJSON.put("equipBrand", operate.getEquipBrand());
				operateJSON.put("opteRemark", operate.getOpteRemark());
				operateJSON.put("opteTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(operate.getOpteTime()));
							
				operateJsonArr.add(operateJSON);
			}
			tableData.put("draw", draw);
			tableData.put("recordsTotal", recordsTotal);
			tableData.put("data", operateJsonArr);
			tableData.put("recordsFiltered", recordsTotal);
			return SUCCESS;
		}
		
		//用户日志信息类型转换
		private UserOperateWeb passUserOperateWeb(UserOperate u){
			UserOperateWeb uw = new UserOperateWeb();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			uw.setId(u.getId());
			uw.setOptePerson(u.getOptePerson());
			if(u.getOpteGenre() != null){
				uw.setOpteGenre(UserOperate.OpteGenre.getName(u.getOpteGenre()));
			}
			uw.setUserCode(u.getUserCode());
			uw.setUserName(u.getUserName());
			uw.setOpteTime(sdf.format(u.getOpteTime()));
			uw.setOpteRemark(u.getOpteRemark());
			return uw;	
		}
		
		//用户操作日志明细
		public String userOperateInfo(){
			try {
				useroperate = useropteService.getUserOpteByKey(id);
				userOperateweb = passUserOperateWeb(useroperate);
			} catch (Exception e) {
				e.printStackTrace();
				getSession().setAttribute("e", e.getMessage());
				return "sysError";
			}			
			return SUCCESS;	
		}
		

		//用户操作日志列表界面数据加载
			public String userListOperateData() {
				
				userOperateList = useropteService.listUserOptePage(temp, start, length,opteGenre,opteTime1,opteTime2);
				System.out.println(userOperateList);
				Integer recordsTotal = useropteService.listUserOptePage(temp,null, null,opteGenre,null,null).size();
				tableData = new JSONObject();
				JSONArray operateJsonArr = new JSONArray();
				for (UserOperate operate : userOperateList) {
					JSONObject operateJSON = new JSONObject();
					operateJSON.put("id", operate.getId());
					operateJSON.put("optePerson", operate.getOptePerson());
					operateJSON.put("opteGenre", UserOperate.OpteGenre.getName(operate.getOpteGenre()));
					operateJSON.put("userCode", operate.getUserCode());
					operateJSON.put("userName", operate.getUserName());					
					operateJSON.put("opteRemark", operate.getOpteRemark());
					operateJSON.put("opteTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(operate.getOpteTime()));
								
					operateJsonArr.add(operateJSON);
				}
				tableData.put("draw", draw);
				tableData.put("recordsTotal", recordsTotal);
				tableData.put("data", operateJsonArr);
				tableData.put("recordsFiltered", recordsTotal);
				return SUCCESS;
			}
			
			//资产操作历史列表界面数据加载
			public String historyListOperateData() {			
				assetUseHistoryList = assetUseHistoryService.getHistoryByKey(id);
				System.out.println(assetUseHistoryList.toString());
				Integer recordsTotal = assetUseHistoryService.getHistoryByKey(id).size();
				tableData = new JSONObject();
				JSONArray operateJsonArr = new JSONArray();
				for (AssetUseHistory assetUseHistory : assetUseHistoryList) {					
					JSONObject operateJSON = new JSONObject();
					operateJSON.put("id", assetUseHistory.getId());					
					operateJSON.put("assetId", assetUseHistory.getAssetId());
					operateJSON.put("usePerson", assetUseHistory.getUsePerson());
					operateJSON.put("userCode", assetUseHistory.getUserCode());					
					operateJSON.put("assignTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(assetUseHistory.getAssignTime()));					
					if(assetUseHistory.getReceiveTime() != null){
						operateJSON.put("receiveTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(assetUseHistory.getReceiveTime()));						
					}else{
						operateJSON.put("receiveTime", null);	
					}
					if(assetUseHistory.getRevertTime() != null){
						operateJSON.put("revertTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(assetUseHistory.getRevertTime()));							
					}else{
						operateJSON.put("revertTime", null);
					}
					if(assetUseHistory.getEquipState() != null){
						operateJSON.put("equipState", AssetUseHistory.EquipState.getName(assetUseHistory.getEquipState()));
					}else{
						operateJSON.put("equipState", null);
					}
					
								
					operateJsonArr.add(operateJSON);
				}
				tableData.put("draw", draw);
				tableData.put("recordsTotal", recordsTotal);
				tableData.put("data", operateJsonArr);
				tableData.put("recordsFiltered", recordsTotal);
				return SUCCESS;
			}

			public AssetOperate getAssetoperate() {
				return assetoperate;
			}

			public void setAssetoperate(AssetOperate assetoperate) {
				this.assetoperate = assetoperate;
			}

			public AssetOperateWeb getAssetOperateweb() {
				return assetOperateweb;
			}

			public void setAssetOperateweb(AssetOperateWeb assetOperateweb) {
				this.assetOperateweb = assetOperateweb;
			}

			public UserOperate getUseroperate() {
				return useroperate;
			}

			public void setUseroperate(UserOperate useroperate) {
				this.useroperate = useroperate;
			}

			public UserOperateWeb getUserOperateweb() {
				return userOperateweb;
			}

			public void setUserOperateweb(UserOperateWeb userOperateweb) {
				this.userOperateweb = userOperateweb;
			}

			public Long getId() {
				return id;
			}

			public void setId(Long id) {
				this.id = id;
			}
			
			public Short getOpteGenre() {
				return opteGenre;
			}

			public void setOpteGenre(Short opteGenre) {
				this.opteGenre = opteGenre;
			}

			public List<AssetOperate> getAssetOperateList() {
				return assetOperateList;
			}

			public void setAssetOperateList(List<AssetOperate> assetOperateList) {
				this.assetOperateList = assetOperateList;
			}

			public List<UserOperate> getUserOperateList() {
				return userOperateList;
			}

			public void setUserOperateList(List<UserOperate> userOperateList) {
				this.userOperateList = userOperateList;
			}

			public JSONObject getTableData() {
				return tableData;
			}

			public void setTableData(JSONObject tableData) {
				this.tableData = tableData;
			}

			public String getTemp() {
				return temp;
			}

			public void setTemp(String temp) {
				this.temp = temp;
			}

			public Integer getDraw() {
				return draw;
			}

			public void setDraw(Integer draw) {
				this.draw = draw;
			}

			public Integer getLength() {
				return length;
			}

			public void setLength(Integer length) {
				this.length = length;
			}

			public Integer getStart() {
				return start;
			}

			public void setStart(Integer start) {
				this.start = start;
			}

			public Date getOpteTime1() {
				return opteTime1;
			}

			public void setOpteTime1(Date opteTime1) {
				this.opteTime1 = opteTime1;
			}

			public Date getOpteTime2() {
				return opteTime2;
			}

			public void setOpteTime2(Date opteTime2) {
				this.opteTime2 = opteTime2;
			}

			public String getOperationResult() {
				return operationResult;
			}

			public void setOperationResult(String operationResult) {
				this.operationResult = operationResult;
			}

}
